﻿using AutoMapper;
using Itech_Run.Application.Contracts.Shared.Dto;
using Itech_Run.Sys.Application.Contracts.Dto;
using Itech_Run.Sys.Domain.Entity;

namespace Itech_Run.Sys.Application
{
    public class Itech_RunSysApplicationAutoMapperProfile : Profile
    {
        public Itech_RunSysApplicationAutoMapperProfile()
        {
            //参考：Value Converters
            //https://docs.automapper.org/en/stable/Value-converters.html?highlight=ForMember#value-converters

            CreateMap<Sys_User, SysUserDto>().ForMember(dest => dest.LookupSysRole, options =>
            {
                options.MapFrom((user, userDto) =>
                {
                    if (user.Sys_UserRole.Any(p => p.Role != null))
                        return user.Sys_UserRole.Where(p => p.Role != null).Select(s => new LookupSysRole
                        {
                            Name = s.Role.Name,
                            Id = s.RoleId
                        });
                    return default;
                });
            });

            CreateMap<SysUserCreateDto, Sys_User>();

            CreateMap<SysUserUpdateDto, Sys_User>();

            CreateMap<Sys_Module, SysModuleDto>().ForMember(dest => dest.LookupSysButton, options =>
            {
                options.MapFrom((entity, entityDto) =>
                {
                    if (entity.Sys_ModuleButton.Any(p => p.Button != null))
                        return entity.Sys_ModuleButton.Where(p => p.Button != null).Select(s => new KeyValueDto
                        {
                            Value = s.Button.Name,
                            Key = s.ButtonId
                        });
                    return default;
                });
            }).ForMember(dest => dest.ParentName, options =>
            {
                options.MapFrom((entity, entityDto) =>
                {
                    if (entity.Parent != null)
                        return entity.Parent.Name;
                    return "";
                });
            }).ForMember(dest => dest.LookupChilds, options =>
            {
                options.MapFrom((entity, entityDto) =>
                {
                    if (entity.Childs != null)
                        return entity.Childs.Select(s => new KeyValueDto
                        {
                            Key = s.Id,
                            Value = s.Name
                        });
                    return new List<KeyValueDto>();
                });
            });

            CreateMap<SysModuleCreateDto, Sys_Module>();

            CreateMap<SysModuleUpdateDto, Sys_Module>();

            CreateMap<Sys_Module, SysModuleTreeDto>().ForMember(dest => dest.Label, options =>
            {
                options.MapFrom((entity, entityDto) =>
                {
                    return entity.Name;
                });
            }).ForMember(dest => dest.Children, options =>
            {
                options.MapFrom((entity, entityDto) =>
                {
                    if (entity.Childs != null)
                        return entity.Childs.OrderBy(e => e.Sort).Select(s => new SysModuleTreeDto
                        {
                            Id = s.Id,
                            Label = s.Name,
                            Level = s.Level,
                            Children = s.Childs.Select(sc => new SysModuleTreeDto
                            {
                                Id = sc.Id,
                                Label = sc.Name,
                                Level = sc.Level
                            }).ToList()
                        });
                    return new List<SysModuleTreeDto>();
                });
            });

            CreateMap<Sys_Module, SysModulePermissionTreeDto>().ForMember(dest => dest.Label, options =>
            {
                options.MapFrom((entity, entityDto) =>
                {
                    return entity.Name;
                });
            }).ForMember(dest => dest.ButtonList, options =>
            {
                options.MapFrom((entity, entityDto) =>
                {
                    return entity.Sys_ModuleButton.Select(s => new SysButtonAuthDto
                    {
                        Id = s.ButtonId,
                        Name = s.Button.Name,
                        IsAuth = false
                    });
                });
            }).ForMember(dest => dest.Children, options =>
            {
                options.MapFrom((entity, entityDto) =>
                {
                    if (entity.Childs != null)
                        return entity.Childs.OrderBy(e => e.Sort).Select(s => new SysModulePermissionTreeDto
                        {
                            Id = s.Id,
                            Label = s.Name,
                            Level = s.Level,
                            ButtonList = s.Sys_ModuleButton.Select(s => new SysButtonAuthDto
                            {
                                Id = s.ButtonId,
                                Name = s.Button.Name,
                                IsAuth = false
                            }).ToList(),
                            Children = s.Childs.Select(sc => new SysModulePermissionTreeDto
                            {
                                Id = sc.Id,
                                Label = sc.Name,
                                Level = sc.Level,
                                ButtonList = sc.Sys_ModuleButton.Select(s => new SysButtonAuthDto
                                {
                                    Id = s.ButtonId,
                                    Name = s.Button.Name,
                                    IsAuth = false
                                }).ToList()
                            }).ToList()
                        });
                    return new List<SysModulePermissionTreeDto>();
                });
            });

            CreateMap<Sys_Role, SysRoleDto>();
            CreateMap<SysRoleCreateDto, Sys_Role>();
            CreateMap<SysRoleUpdateDto, Sys_Role>();

            CreateMap<Sys_Dictionary, SysDictionaryDto>();
            CreateMap<SysDictionaryCreateDto, Sys_Dictionary>();
            CreateMap<SysDictionaryUpdateDto, Sys_Dictionary>();
        }
    }
}
